<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTypeEnum == DialogType.edit ? '修改菜单' : '添加菜单'"
    :destroy-on-close="true"
    width="580px"
    draggable
  >
    <el-form class="drawer-multiColumn-form" label-width="120px" ref="formRef" :rules="rules" :model="ruleForm">
      <el-form-item label="菜单名称" prop="meta.title">
        <el-input v-model="ruleForm.meta.title" placeholder="路由标题" clearable></el-input>
      </el-form-item>
      <el-form-item label="路由访问路径" prop="path">
        <el-input
          v-model="ruleForm.path"
          placeholder="路由访问路径"
          clearable
          :disabled="dialogTypeEnum == DialogType.Null"
        ></el-input>
      </el-form-item>

      <el-form-item label="路由名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="路由名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="重定向路径" prop="redirect">
        <el-input v-model="ruleForm.redirect" placeholder="重定向路径" clearable></el-input>
      </el-form-item>

      <el-form-item label="组件路径" prop="component">
        <el-input
          v-model="ruleForm.component"
          placeholder="组件路径"
          clearable
          :disabled="dialogTypeEnum == DialogType.Null"
        ></el-input>
      </el-form-item>

      <el-form-item label="图标" prop="meta.icon">
        <el-input v-model="ruleForm.meta.icon" placeholder="图标" clearable></el-input>
      </el-form-item>

      <el-form-item label="是否隐藏" prop="meta.isHide">
        <el-switch v-model="ruleForm.meta.isHide" active-text="是" inactive-text="否"></el-switch>
      </el-form-item>

      <el-form-item label="是否外链" prop="meta.isLink">
        <el-switch v-model="ruleForm.meta.isLink" active-text="是" inactive-text="否"></el-switch>
      </el-form-item>

      <el-form-item label="是否固定" prop="meta.isAffix">
        <el-switch v-model="ruleForm.meta.isAffix" active-text="是" inactive-text="否"></el-switch>
      </el-form-item>

      <el-form-item label="是否全屏" prop="meta.isFull">
        <el-switch v-model="ruleForm.meta.isFull" active-text="是" inactive-text="否"></el-switch>
      </el-form-item>

      <el-form-item label="是否缓存" prop="meta.isCache">
        <el-switch v-model="ruleForm.meta.isCache" active-text="是" inactive-text="否"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="menuDialog">
// 11
import { reactive, ref } from "vue";
import { FormInstance } from "element-plus";
import { RouteRecordRaw } from "vue-router";
import { clearObject } from "@/utils";
import { DialogType } from "@/utils/dict";

const emit = defineEmits<{
  success: [value: RouteRecordRaw, dialogType: DialogType];
}>();

const ruleForm = reactive<RouteRecordRaw>({
  path: "",
  name: "",
  component: "",
  redirect: "",
  meta: {
    icon: "Menu",
    title: "",
    isLink: "",
    isHide: false,
    isFull: false,
    isAffix: false,
    isKeepAlive: true
  },
  children: []
});

const rules = reactive({
  name: [{ required: true, message: "请输入名称" }],
  path: [{ required: true, message: "不能为空" }],
  component: [{ required: true, message: "不能为空" }],
  meta: {
    title: [{ required: true, message: "不能为空" }]
  }
});

// dialog状态
const dialogVisible = ref(false);
// 父组件传过来的参数

const formRef = ref<FormInstance>();
const submit = () => {
  formRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      emit("success", ruleForm, dialogTypeEnum.value);
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
// dialog状态
const dialogTypeEnum = ref<DialogType>();

const show = (row: RouteRecordRaw, dialogType: DialogType) => {
  clearObject(ruleForm);
  Object.assign(ruleForm, row);
  dialogVisible.value = true;
  dialogTypeEnum.value = dialogType;
};

defineExpose({
  show
});
</script>
<style lang="scss" scoped></style>
